﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ACLibs.Config
{
    /// <summary>
    /// 自定义配置文件操作方法
    /// </summary>
    public class CustomConfig
    {
        #region 定义类
        /// <summary>
        /// 自定义节
        /// </summary>
        public sealed class CustomSection : ConfigurationSection
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public CustomSection()
            {
                Name = "ACCustomSection";
                Value = "";
            }

            #region 节属性设置
            #endregion

            #region 方法设置
            /// <summary>
            /// 属性-名称
            /// </summary>
            [ConfigurationProperty("name", DefaultValue = "ACCustomSection", IsRequired = true, IsKey = true)]
            public string Name
            {
                get { return (string)this["name"]; }
                set { this["name"] = value; }
            }

            /// <summary>
            /// 属性-内容
            /// </summary>
            [ConfigurationProperty("value", DefaultValue = "", IsRequired = false)]
            public string Value
            {
                get { return (string)this["value"]; }
                set { this["value"] = value; }
            }
            #endregion

            #region 节内部元素
            /// <summary>
            /// 自定义节所有元素
            /// </summary>
            [ConfigurationProperty("values", IsDefaultCollection = false)]
            [ConfigurationCollection(typeof(CustomAppElementCollection), AddItemName = "add", ClearItemsName = "clear")]
            public CustomAppElementCollection Values
            {
                get
                {
                    CustomAppElementCollection customAppElementCollection = (CustomAppElementCollection)base["values"];
                    return customAppElementCollection;
                }
                set
                {
                    CustomAppElementCollection customAppElementCollection = value;
                }
            }
            #endregion
        }

        /// <summary>
        /// 自定义配置文件元素
        /// </summary>
        public sealed class CustomAppElement : ConfigurationElement
        {
            public CustomAppElement()
            {

            }

            public CustomAppElement(int id, string name, string ip, int port, int password)
            {
                this.ID = id;
                this.Name = name;
                this.IP = ip;
                this.Port = port;
                this.Password = password;
            }

            [ConfigurationProperty("id", DefaultValue = "0", IsRequired = true, IsKey = true)]
            public int ID
            {
                get { return (int)this["id"]; }
                set { this["id"] = value; }
            }
            [ConfigurationProperty("name", DefaultValue = "0")]
            public string Name
            {
                get { return (string)this["name"]; }
                set { this["name"] = value; }
            }
            [ConfigurationProperty("ip", DefaultValue = "0.0.0.0")]
            public string IP
            {
                get { return (string)this["ip"]; }
                set { this["ip"] = value; }
            }
            [ConfigurationProperty("port", DefaultValue = "4173")]
            public int Port
            {
                get { return (int)this["port"]; }
                set { this["port"] = value; }
            }
            [ConfigurationProperty("password", DefaultValue = "0")]
            public int Password
            {
                get { return (int)this["password"]; }
                set { this["password"] = value; }
            }
        }

        /// <summary>
        /// 自定义配置文件元素集
        /// </summary>
        public sealed class CustomAppElementCollection : ConfigurationElementCollection
        {
            public CustomAppElementCollection()
            {

            }

            public override ConfigurationElementCollectionType CollectionType
            {
                get
                {
                    return ConfigurationElementCollectionType.AddRemoveClearMap;
                }
            }

            protected override ConfigurationElement CreateNewElement()
            {
                return new CustomAppElement();
            }

            protected override Object GetElementKey(ConfigurationElement element)
            {
                return ((CustomAppElement)element).ID;
            }

            public CustomAppElement this[int index]
            {
                get
                {
                    return (CustomAppElement)BaseGet(index);
                }
                set
                {
                    if (BaseGet(index) != null)
                    {
                        BaseRemoveAt(index);
                    }
                    BaseAdd(index, value);
                }
            }

            new public CustomAppElement this[string ID]
            {
                get
                {
                    return (CustomAppElement)BaseGet(ID);
                }
            }

            public void Add(CustomAppElement element)
            {
                BaseAdd(element);
            }
            protected override void BaseAdd(ConfigurationElement element)
            {
                BaseAdd(element, false);
                // 这里写自定义代码
            }

            public void Clear()
            {
                BaseClear();
            }
        }
        #endregion
        /// <summary>
        /// 在配置文件中以默认值创建自定义节
        /// </summary>
        public static void CreateCustomAppSectionToConfigurationFile()
        {
            CustomSection customAppSection = new CustomSection();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.Sections["CustomAppSection"] == null)
            {
                config.Sections.Add("CustomAppSection", customAppSection);
            }
            config.Save();
        }
        /// <summary>
        /// 获取自定义配置节
        /// </summary>
        public static CustomSection GetCustomAppSection()
        {
            CustomSection customAppSection;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            customAppSection = config.GetSection("CustomAppSection") as CustomSection;
            return customAppSection;
        }

        /// <summary>
        /// 另存为
        /// </summary>
        public static void SaveConfigurationFile(string FileName)
        {
            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SaveAs(FileName);
        }

        /// <summary>
        /// 获取属性-名称
        /// </summary>
        /// <returns></returns>
        public static string GetCustomAppSectionName()
        {
            CustomSection customAppSection;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.Sections["CustomAppSection"] == null)
            {
                return "";
            }
            else
            {
                customAppSection = config.GetSection("CustomAppSection") as CustomSection;
                return customAppSection.Name;
            }
        }

        /// <summary>
        /// 获取属性-内容
        /// </summary>
        /// <returns></returns>
        public static string GetCustomAppSectionValue()
        {
            CustomSection customAppSection;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.Sections["CustomAppSection"] == null)
            {
                return "";
            }
            else
            {
                customAppSection = config.GetSection("CustomAppSection") as CustomSection;
                return customAppSection.Value;
            }
        }
    }
}
